package com.server.timertask;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author	计时器任务
* @email	397554852@qq.com
* @version	创建时间：2018年04月04日 上午10:18:27
*/
public class LmyScheduledTask {
	private static final Logger logger = LoggerFactory.getLogger(LmyScheduledTask.class);
	
	private static final int PERFORM_ONCE_ASSIGNDAY = 0X0001;
	private static final int PERFORM_REPEATEDLY = 0X0002;
	
	private String className = "com.server.timertask.TimerTaskFunction";
	
	private int type;
	
	private int count = 0;
	private int period = 1000 * 60 * 60 * 24;
	
	private TimerTask task;
	private Object obj;
	private Method m;
	
	private int Year;
	private int Month;
	private int Day;
	private int Hour;
	private int Minute;
	private int Second;
	
	/**
	 * 指定日期执行一次
	 * @param className 包名+类名
	 * @param methodName 方法名
	 * @param Day 年
	 * @param Month 月
	 * @param Day 日
	 * @param Hour 时
	 * @param Minute 分
	 * @param Second 秒
	 */
	public LmyScheduledTask(String className, String methodName,int Year, int Month, int Day, int Hour, int Minute ,int Second) {
		type = PERFORM_ONCE_ASSIGNDAY;
		if(className == null)className = this.className;
		this.Year = Year;
		this.Month = Month;
		this.Day = Day;
		this.Hour = Hour;
		this.Minute = Minute;
		this.Second = Second;
		try {
			obj = Class.forName(className).newInstance();
			m = obj.getClass().getMethod(methodName);
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 固定时间开始，每间隔Period(ms)时间执行一次
	 * @param className 方法类名
	 * @param methodName 方法名
	 * @param Hour 定时的小时
	 * @param Minute 定时的分钟
	 * @param Second 定时的秒钟
	 * @param period 间隔的时间，单位ms 输入0则每间隔24小时执行一次
	 */
	public LmyScheduledTask(String className, String methodName,int Hour, int Minute ,int Second, int period) {
		type = PERFORM_REPEATEDLY;
		if(className == null)className = this.className;
		this.Hour = Hour;
		this.Minute = Minute;
		this.Second = Second;
		try {
			obj = Class.forName(className).newInstance();
			m = obj.getClass().getMethod(methodName);
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		if(period != 0) this.period = period;
	}
	
	/**
	 * 开始执行定时计划
	 */
	@SuppressWarnings("deprecation")
	public void Start() {
		task = new TimerTask() {
			@Override
			public void run() {
				++count;
				logger.info("Time:" + new Date().toLocaleString() + " Count:" + count); // 1次
				try {
					m.invoke(obj);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		};
		//设置执行时间
		Calendar calendar = Calendar.getInstance();
		if(type == PERFORM_REPEATEDLY) {
		    Year = calendar.get(Calendar.YEAR);
		    Month = calendar.get(Calendar.MONTH);
		    Day = calendar.get(Calendar.DAY_OF_MONTH);
		    
		}
	    calendar.set(Year, Month, Day, Hour, Minute, Second);
	    Date date = calendar.getTime();
	    Timer timer = new Timer();
    	
	    switch(type) {
	    case PERFORM_ONCE_ASSIGNDAY:
	    	timer.schedule(task, date);
	    	break;
	    case PERFORM_REPEATEDLY:
		    timer.scheduleAtFixedRate(task, date, period);
	    	break;
	    }
	}
	/**
	 * 取消定时计划
	 */
	public void Stop() {
		task.cancel();
	}
}
